<?php

namespace app\api\controller\supplier;

use addons\wechat\library\Config as ConfigService;
use AlibabaCloud\SDK\Iot\V20180120\Iot;
use AlibabaCloud\SDK\Iot\V20180120\Models\PubRequest;
use app\admin\model\AssistantOrder;
use app\admin\model\Chargecode;
use app\admin\model\Cmdlog;
use app\admin\model\Devicelock;
use app\admin\model\Linedevice;
use app\admin\model\litestore\Litestoreorder;
use app\admin\model\litestore\Litestoreorderaddress;
use app\admin\model\litestore\Litestoreordergoods;
use app\admin\model\Mallsupplier;
use app\admin\model\Org;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdaytrade;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptableorder;
use app\admin\model\Supplierprofit;
use app\admin\model\Supplierwithdraw;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;
use app\api\controller\Exception;
use App\Api\PrinterService;
use App\Api\PrintService;
use app\api\service\AirService;
use app\api\service\BalanceService;
use app\api\service\DeviceService;
use app\api\service\LockService;
use app\api\service\MpService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\RoomService;
use app\api\service\VbillService;
use app\api\service\XhtMtService;
use app\common\controller\Api;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use Darabonba\OpenApi\Models\Config;
use EasyWeChat\Factory;
use fast\Http;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\MQTTClient;
use think\Console;
use think\exception\HttpResponseException;
use think\Response;
use Workerman\Mqtt\Client;
use Workerman\Worker;

/**
 * 首页接口
 */
class Money extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];




    public function incomeList()
    {

        $status = $this->request->param('status');
        $page = $this->request->param('page');
        $user = $this->auth->getUser();
        $supplierProfitModel = new Supplierprofit();

        $limit = 10;
        $offset = ($page - 1) * $limit;


        $where = array();
        $where['user_id'] = $user['id'];

        if($status != 0){
            $where['status'] = $status;
        }

        $list = $supplierProfitModel->where($where)->limit($offset,$limit)->order('create_time','desc')->select();

        foreach ($list as $key=>$val){
            if($val['status'] == -1){
                $list[$key]['status_txt'] = "未结算";
            }else{
                $list[$key]['status_txt'] = "已结算";
            }
        }
        $total = $supplierProfitModel->where($where)->count('id');

        $ret = array();
        $ret['total'] =$total;
        $ret['list'] =$list;
        $this->success('',$ret);


    }



    public function withList()
    {

        $status = $this->request->param('status');
        $page = $this->request->param('page');
        $user = $this->auth->getUser();
        $withListModel = new Supplierwithdraw();

        $limit = 10;
        $offset = ($page - 1) * $limit;


        $where = array();
        $where['user_id'] = $user['id'];

        if($status != 0){
            $where['status'] = $status;
        }

        $list = $withListModel->where($where)->limit($offset,$limit)->order('create_time','desc')->select();

        foreach ($list as $key=>$val){
            if($val['status'] == 'doing'){
                $list[$key]['status_txt'] = "处理中";
            }else{
                $list[$key]['status_txt'] = "处理完成";
            }
            $list[$key]['img']= cdnurl($val['img'],true);
            $list[$key]['create_time'] = date("Y-m-d H:i",$val['create_time']);
        }
        $total = $withListModel->where($where)->count('id');

        $ret = array();
        $ret['total'] =$total;
        $ret['list'] =$list;
        $this->success('',$ret);


    }

    public function doWith()
    {

        $user = $this->auth->getUser();

        $mallSupplierModel = new Mallsupplier();
        $supplier = $mallSupplierModel->where('mobile',$user['mobile'])->find();

        $user = $this->auth->getUser();
        $withListModel = new Supplierwithdraw();
        $amount = $this->request->param('amount');
        $img = $this->request->param('img');

        $supplierProfitModel = new Supplierprofit();
        $profit = $supplierProfitModel->where('user_id',$user['id'])->where('status',1)->sum('amount');

        $witAmount = $withListModel->where('user_id',$user['id'])->where('status','in',['doing','done'])->sum('amount');
        $balance = $profit-$witAmount;
        if($amount >$balance) $this->error("余额不足");

        $withListModel->insert(['user_id'=>$user['id'],'amount'=>$amount,'bank_card'=>$supplier['bank_card'],'img'=>$img,'status'=>'doing','create_time'=>time()]);

        $this->success('成功');
    }

    public function wallat()
    {

        $user = $this->auth->getUser();
        $supplierProfitModel = new Supplierprofit();
        $profit = $supplierProfitModel->where('user_id',$user['id'])->where('status',1)->sum('amount');
        $withListModel = new Supplierwithdraw();

        $witAmount = $withListModel->where('user_id',$user['id'])->where('status','in',['doing','done'])->sum('amount');
        $balance = $profit-$witAmount;

        $djAmount = $supplierProfitModel->where('user_id',$user['id'])->where('status',-1)->sum('amount');



        $mallSupplierModel = new Mallsupplier();
        $supplier = $mallSupplierModel->where('mobile',$user['mobile'])->find();


        $ret = array();
        $ret['balance'] = $balance;
        $ret['djAmount'] = $djAmount;
        $ret['card'] = $supplier['bank_card'];

        $this->success('',$ret);

    }


}

